package com.itheima.reggie.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dao.CategoryDao;
import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.dao.SetmealDao;
import com.itheima.reggie.dao.SetmealDishDao;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.exception.CustomerException;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.HeuristicCompletionException;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealDao setmealDao;
    @Autowired(required = false)
    private DishDao dishDao;
    @Autowired(required = false)
    private SetmealDishDao setmealDishDao;
    @Autowired(required = false)
    private CategoryDao categoryDao;

    /***
     * 保存套餐
     */
    @Override
    @Transactional
    @CacheEvict(value = "setmeal",allEntries = true)
    public void save(SetmealDto setmealDto) {
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setCreateTime(LocalDateTime.now());
//        setmealDto.setStatus(1);
        setmealDao.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCopies(1);
            setmealDish.setSort(0);
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
        }
        setmealDishDao.batchSave(setmealDishes);
    }

    @Override
    public Page<SetmealDto> getList(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<Setmeal>setmeals= setmealDao.findSetmealById(name);

        PageInfo<Setmeal>pageInfo=new PageInfo<>(setmeals);
        List<SetmealDto> setmealDtoList =new ArrayList<>();
        for (Setmeal setmeal : setmeals) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryDao.findNameById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        }
        Page<SetmealDto>pageList=new Page<>(setmealDtoList,pageInfo.getTotal(),pageInfo.getPageSize(),pageInfo.getPageNum());

        return pageList;
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
//        如果商品在起售状态不能删除
        Integer count =setmealDao.findStatusByIds(ids);
        if (count>0){
            throw new CustomerException("起售状态不能删除");
        }
//        删除套餐表数据
        setmealDao.batchDelete(ids);
//        删除套餐里边的详细数据
        setmealDishDao.batchDelete(ids);
    }

    @Override
    @Cacheable(value = "setmeal",key = "#categoryId+'_'+#status",unless = "#categoryId==null")
    public List<SetmealDto> list(Long categoryId, Integer status) {
        List<Setmeal>setmealList=setmealDao.findAllBycategoryId(categoryId,status);
        List<SetmealDto> setmealDtolist = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            List<SetmealDish> setmealDishList = setmealDishDao.findAll(setmeal.getId());
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }).collect(Collectors.toList());
        return setmealDtolist;
    }

    @Override
    public List<SetmealDishVo> findAll(Long setmealId) {
        List<SetmealDish> setmealDishList = setmealDishDao.findAllBysid(setmealId);
        List<SetmealDishVo> setmealDishVos = setmealDishList.stream().map(setmealDish -> {
            SetmealDishVo setmealDishVo = new SetmealDishVo();
            BeanUtils.copyProperties(setmealDish,setmealDishVo);
            Dish dish = dishDao.findAllById(setmealDish.getDishId());
            setmealDishVo.setImage(dish.getImage());
            return setmealDishVo;
        }).collect(Collectors.toList());
        return setmealDishVos;
    }
}
